package org.thirdHero.wx.request;

import com.github.binarywang.wxpay.bean.request.WxPayBaseRequest;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import org.apache.commons.lang3.StringUtils;
import org.thirdHero.wx.utils.WxSignUtils;

public abstract class WxProfitSharingBaseRequest extends WxPayBaseRequest {
    /**
     * <pre>
     * 字段名：签名类型
     * 变量名：sign_type
     * 是否必填：签名类型
     * 类型：string(32)
     * 示例值：HMAC-SHA256
     * 描述：HMAC-SHA256
     * </pre>
     */
    @XStreamOmitField
    private String signType;
    @Override
    public void checkAndSign(WxPayConfig config) throws WxPayException {
        this.checkConstraints();
        if (StringUtils.isBlank(getAppid())) {
            this.setAppid(config.getAppId());
        }

        if (StringUtils.isBlank(getMchId())) {
            this.setMchId(config.getMchId());
        }

        if (StringUtils.isBlank(getSubAppId())) {
            this.setSubAppId(config.getSubAppId());
        }

        if (StringUtils.isBlank(getSubMchId())) {
            this.setSubMchId(config.getSubMchId());
        }

        if (StringUtils.isBlank(getNonceStr())) {
            this.setNonceStr(String.valueOf(System.currentTimeMillis()));
        }
        this.setSign(WxSignUtils.createSign(this, config.getMchKey(), signType));
    }
    @Override
    public String getSignType() {
        return signType;
    }

    @Override
    public void setSignType(String signType) {
        this.signType = signType;
    }
}
